package by.grsu.coctails.fragments;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import by.grsu.coctails.R;
import by.grsu.coctails.adapter.AbstractAdapter;
import by.grsu.coctails.adapter.CoctailAdapter;
import by.grsu.coctails.db.task.CommonGetAsyncTask;
import by.grsu.coctails.db.task.GetCoctailsAsyncTask;
import by.grsu.coctails.model.Coctail;

public class ListCoctailFragment extends CommonDBFragment<Coctail> {

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		getListView().setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				Coctail coctail = getAdapter().getItem(position);
				FragmentCreator.addCompostionFragment(getActivity(), coctail);
			}
		});

	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public CommonGetAsyncTask getTask(String categoryName) {
		return new GetCoctailsAsyncTask(getActivity(), categoryName) {

			@Override
			public void onComplete(List<Coctail> result) {
				sucess(result);
			}
		};
	}

	public void sucess(List<Coctail> result) {
		getAdapter().getList().addAll(result);
		getAdapter().notifyDataSetChanged();
	}

	@Override
	public int getLayoutResource() {
		return R.layout.list_coctails_fragment;
	}

	@Override
	public AbstractAdapter<Coctail> createAdapter() {
		List<Coctail> list = new ArrayList<Coctail>();
		return new CoctailAdapter(getActivity(), R.layout.coctail_item, list);
	}

	@Override
	public String getTitle() {
		return getString(R.string.all_coctails);
	}

}
